package java_thinking.unit_17._17_2_6;

import java_thinking.net.mindview.util.CountingGenerator;
import java_thinking.net.mindview.util.RandomGenerator;
import java_thinking.unit_15._15_3.Generator;

import java.util.Iterator;
import java.util.LinkedHashMap;

/**
 * @Desc:   3 Map 生成器
 * @author: cww
 * @DateTime: 2019-04-10 23:48
 */
class Pair<K,V> {
    /**
     * 都是public final是为了成为只读的数据传输对象(或信使)
     */
    public final K key;
    public final V value;

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }
}
public class MapData<K,V> extends LinkedHashMap<K,V> {
    public MapData(Generator<Pair<K,V>> gen,int quantity) {
        for (int i=0;i<quantity;i++) {
            Pair<K,V> pair = gen.next();
            put(pair.key,pair.value);
        }
    }
    public MapData(Generator<K> genK,Generator<V> genV,int quantity) {
        for (int i=0;i<quantity;i++) {
            put(genK.next(),genV.next());
        }
    }
    public MapData(Generator<K> genK,V value,int quantity) {
        for (int i=0;i<quantity;i++) {
            put(genK.next(),value);
        }
    }
    public MapData(Iterable<K> keys,Generator<V> genV) {
        for (K key:keys) {
            put(key,genV.next());
        }
    }
    public MapData(Iterable<K> ks,V value){
        for (K key:ks) {
            put(key,value);
        }
    }
    public static <K,V> MapData<K,V> map(Generator<Pair<K,V>> gen, int quantity) {
        return new MapData<K,V>(gen, quantity);
    }
    public static <K,V> MapData<K,V> map(Generator<K> genK, Generator<V> genV, int quantity) {
        return new MapData<K,V>(genK, genV, quantity);
    }
    public static <K,V> MapData<K,V> map(Generator<K> genK, V value, int quantity) {
        return new MapData<K,V>(genK, value, quantity);
    }
    public static <K,V> MapData<K,V> map(Iterable<K> genK, Generator<V> genV) {
        return new MapData<K,V>(genK, genV);
    }
    public static <K,V> MapData<K,V> map(Iterable<K> genK, V value) {
        return new MapData<K,V>(genK, value);
    }

}

/**
 * MapData实例
 */
class Letters implements Generator<Pair<Integer,String>>,Iterable<Integer> {

    private int size = 9;
    private int number = 1;
    private char letter = 'A';
    @Override
    public Pair<Integer, String> next() {
        return new Pair<>(number++,""+letter++);
    }


    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            @Override
            public boolean hasNext() {
                return number < size;
            }

            @Override
            public Integer next() {
                return number++;
            }
        };
    }
}

class MapDataTest {
    public static void main(String[] args) {
        System.out.println(MapData.map(new Letters(),10));
        System.out.println(MapData.map(new CountingGenerator.Character(),new RandomGenerator.String(3),8));
        System.out.println(MapData.map(new CountingGenerator.Character(),"value",10));
        System.out.println(MapData.map(new Letters(),new RandomGenerator.String(3)));
        System.out.println(MapData.map(new Letters(),"Pop"));
    }
}
